/**
 * 应用商品
 */
$(document).ready(function() {
	var bindModelData = {
		appModuleForm:{name:"",code:""},
		appModuleSearch:{name:""}
	}
	var appModuleSearchFormModel,appModuleDialog,appModuleFormModel,appModuleFormValidator;
	function init(){
		initSearchForm();
		initappModuleForm();
		if($("#search").data("LBSFormSearch")===undefined){
			$("#search").LBSFormSearch({
				searchBar:true,
				searchHandle:searchHandle,
				resetHandle:resetSearchHandle,
				custom:[{name:"add",value:"添加",click:function(e){addModuleHandle(e);},spriteCssClass:"k-icon k-i-plus"}]
			});
		}
		
		if($("#app_module_grid").data("kendoGrid")===undefined){
			$("#app_module_grid").kendoGrid({
				autoBind:true,
		        dataSource: getGridDataSource({
		        	read:{
		        		url:"/system/appModule/find",
		        	},
		        	model:{
		        		id:"id",
		        		fields:{
		        			id:{type:"string"},
		        			name:{type:"string"},
		        			createTime:{type:"date"},
		        			modifyTime:{type:"date"}
		        		}
		        	}
		        }),
		        sortable: true,
		        selectable: true,
		        editable: false,
		        resizable: true,
		        pageable: true,
		        dataBinding: function() {
		            record = (this.dataSource.page() -1) * this.dataSource.pageSize();
		        },
		        columns: [
		            {title: "序号",template: "#= ++record #",width: 50},
		            {field: "id",title: "编号",hidden:true}, 
		            {field: "name",title: "名称"}, 
		            {field: "code",title: "编码"},
		            {field: "createTime",title: "创建时间",format: "{0: yyyy-MM-dd HH:mm:ss}"}
		        ]
		    });
		}
	}
	function initSearchForm(){
		if($("input[name='name']",$("#search")).data("kendoMaskedTextBox")===undefined){
			$("input[name='name']",$("#search")).kendoMaskedTextBox();
		}
		appModuleSearchFormModel = kendo.observable(bindModelData.appModuleSearch);
		kendo.bind("#search", appModuleSearchFormModel);
	}
	function initappModuleForm(){
		appModuleFormValidator = $("#app_module_form").kendoValidator().data("kendoValidator");
		if($("#app_module_form").data("LBSDialog")===undefined){
			appModuleDialog = $("#app_module_form").LBSDialog({
				height:200,
				width:350,
				deactivate:resetFormHandle,
				bbars:{
					save:saveFormHandle
				}
			});
		}
		if($("input[name='name']",$("#app_module_form")).data("kendoMaskedTextBox")===undefined){
			$("input[name='name']",$("#app_module_form")).kendoMaskedTextBox();
		}
		if($("input[name='code']",$("#app_module_form")).data("kendoMaskedTextBox")===undefined){
			$("input[name='code']",$("#app_module_form")).kendoMaskedTextBox();
		}
	}
	function main(){
		init();
	}
	function searchHandle(){
		$("#app_module_grid").data("kendoGrid").dataSource.read({content:kendo.stringify(appModuleSearchFormModel)});
	}
	function resetSearchHandle(){
		appModuleSearchFormModel = kendo.observable(bindModelData.appModuleSearch);
		kendo.bind("#search", appModuleSearchFormModel);
	}
	function addModuleHandle(){
		appModuleFormModel = kendo.observable(bindModelData.appModuleForm);
		kendo.bind("#app_module_form", appModuleFormModel);
		appModuleDialog.open();
	}
	function resetFormHandle(){
		appModuleFormModel = kendo.observable(bindModelData.appModuleForm);
		kendo.bind("#app_module_form", appModuleFormModel);
	}
	function saveFormHandle(){
		var url = "/system/appModule/save";
		if(this.paramters!==undefined){url = this.paramters.url}
		if(appModuleFormValidator.validate()){
			httpRequestHandle({
				url:url,
				data:{content:kendo.stringify(appModuleFormModel)}
			}).done(function(response){
				$("#app_module_grid").data("kendoGrid").dataSource.read();
			});
			this.close();
		}else{
			LBSNotification({message:"检查表格字段是否为空"});
		}
		delete this.paramters;
	}
	main();
});